package com.abo.yaas.i18n;

import java.io.File;
import java.io.FileFilter;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;

public class TranslationLoader implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        
        final File dir = new File(arg0.getServletContext().getRealPath("/") + "i18n");

        final File[] files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                final String path = pathname.getAbsolutePath();
                final int mid = path.lastIndexOf(".");
                final String ext = path.substring(mid + 1, path.length());
                return "xml".equals(ext);
            }
        });

        if (dir.isDirectory()) {
            for (final File file : files) {
                final TranslationSource src = new TmxSourceReader(file);
                InternationalizationServlet.loadTranslations(src);
            }
        }
    }
}
